package org.fyp.synchrona.services;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.fyp.synchrona.core.info.impl.CommonDataObject;

import com.google.gdata.client.GoogleAuthTokenFactory;
import com.google.gdata.util.AuthenticationException;

public class BloggerProfiler implements Runnable{
	
	private CommonDataObject cpo;
	
	public BloggerProfiler(){
		
	}

	@Override
	public void run()  {
		// TODO Auto-generated method stub
		String username = "synchrona.group@gmail.com";
		String password = "pass123456";
		String serviceName = "blogger";

		GoogleAuthTokenFactory factory = new GoogleAuthTokenFactory(
				serviceName, "", null);
		// Obtain authentication token from Google Accounts
		String token = null;
		try {
			token = factory.getAuthToken(username, password, null, null,
					serviceName, "");
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// System.out.println(token);

		String serviceUrl = "http://www.blogger.com/home";
		String loginUrl = null;
		try {
			loginUrl = "https://www.blogger.com/start?continue="
					+ URLEncoder.encode(serviceUrl, "UTF-8") + "&auth=" + token;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpgetHome = new HttpGet(loginUrl);
		HttpResponse response = null;
		try {
			response = httpclient.execute(httpgetHome);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HttpEntity entity = response.getEntity();
		// System.out.println("Status: " + response.getStatusLine());
		if (entity != null) {
			// System.out.println("Blogger profile page: "+EntityUtils.toString(entity));
			try {
				entity.consumeContent();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		String securityToken = "";
		String security_token = "";
		String USIradio = "";
		String USIerror = "";

		HttpGet httpgetEdit = new HttpGet(
				"http://www.blogger.com/edit-profile.g");
		try {
			response = httpclient.execute(httpgetEdit);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		entity = response.getEntity();
		System.out.println("Status: " + response.getStatusLine());
		if (entity != null) {
			String editPage = null;
			try {
				editPage = EntityUtils.toString(entity);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String[] array = editPage
					.split("securityToken\" type=\"hidden\" value=\"");
			array = array[1].split("\">");
			securityToken = array[0];
			array = editPage.split("security_token\" value=\"");
			array = array[1].split("\">");
			security_token = array[0];
			array = editPage.split("id=\"USI_");
			array = array[1].split("_asynchronous");
			String USI = array[0];
			USIradio = "USI_" + USI + "_imgSrcRadio";
			USIerror = "USI_" + USI + "_imgUrlTextError";
			// System.out.println("1:"+securityToken);
			// System.out.println("2:"+security_token);
			// System.out.println("3:"+USI);
			try {
				entity.consumeContent();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// update profile
		HttpPost httpostProfile = new HttpPost(
				"http://www.blogger.com/edit-profile.do");
		List nvps = new ArrayList();
		nvps.add(new BasicNameValuePair("securityToken", securityToken));
		nvps.add(new BasicNameValuePair("security_token", security_token));
		nvps.add(new BasicNameValuePair("enable", "true"));
		nvps.add(new BasicNameValuePair("showName", "true"));
		nvps.add(new BasicNameValuePair("showEmail", "true"));
		// nvps.add(new BasicNameValuePair("showFollowed","true"));//check
		nvps.add(new BasicNameValuePair("email", "synchrona.group@gmail.com"));
		nvps.add(new BasicNameValuePair("widget.displayname", "Synchrona"));
		nvps.add(new BasicNameValuePair("firstname", "project"));
		nvps.add(new BasicNameValuePair("lastname", "fyp"));

		nvps.add(new BasicNameValuePair("photourl", ""));
		nvps.add(new BasicNameValuePair("photowidth", ""));
		nvps.add(new BasicNameValuePair("photoheight", ""));
		// nvps.add(new BasicNameValuePair("USI_1pqgkpdqj4c5y_imgSrcRadio"
		// ,"on"));
		nvps.add(new BasicNameValuePair("imageFileName", ""));
		nvps.add(new BasicNameValuePair(USIradio, "on"));
		nvps.add(new BasicNameValuePair(USIerror, ""));

		nvps.add(new BasicNameValuePair("audioclipurl", ""));
		if(cpo.getBIO()!=null){
			if(cpo.getBIO()=="1"){
				nvps.add(new BasicNameValuePair("gender", "MALE"));
			}else{
				nvps.add(new BasicNameValuePair("gender", "FEMALE"));
			}
			
		}
		
		//
		if(cpo.getBIRTH_DAY()!=null){
			nvps.add(new BasicNameValuePair("birthday", cpo.getBIRTH_DAY()));
		}
		
		if(cpo.getBIRTH_MONTH()!=null){
			nvps.add(new BasicNameValuePair("birthmonth", cpo.getBIRTH_MONTH()));
		}
		
		
		if(cpo.getBIRTH_YEAR()!=null){
			nvps.add(new BasicNameValuePair("birthyear", cpo.getBIRTH_YEAR()));
		}
		
		
		
		nvps.add(new BasicNameValuePair("showAstrologicalSigns", "true"));
		if(cpo.getWEBSITE()!=null){
			nvps.add(new BasicNameValuePair("url", cpo.getWEBSITE()));
		}
		if(cpo.getWEBSITE()!=null){
			nvps.add(new BasicNameValuePair("wishlisturl", cpo.getWEBSITE()));
		}
		
		nvps.add(new BasicNameValuePair("imusername", ""));
		nvps.add(new BasicNameValuePair("imnetwork", "_nil_"));// JABBER etc
		if(cpo.getCURRENT_COUNTRY_CITY()!=null){
			nvps.add(new BasicNameValuePair("widget.city", cpo.getCURRENT_COUNTRY_CITY()));
		}
			
		if(cpo.getCOUNTRY_CITY()!=null){
			nvps.add(new BasicNameValuePair("widget.state", cpo.getCOUNTRY_CITY()));
		}
		
		nvps.add(new BasicNameValuePair("widget.country", "_nil_"));
		nvps.add(new BasicNameValuePair("ind", "_nil_"));
		nvps.add(new BasicNameValuePair("occupation", "software engineer"));
		nvps.add(new BasicNameValuePair("interests", "engineer"));
		if(cpo.getBIO()!=null){
			nvps.add(new BasicNameValuePair("widget.aboutme",cpo.getBIO()));
		}
		
		nvps.add(new BasicNameValuePair("movies", "MATRIX"));
		nvps.add(new BasicNameValuePair("music", "abc"));
		nvps.add(new BasicNameValuePair("books", "xyz"));
		nvps.add(new BasicNameValuePair("wackyQuestion", "sdfsdf"));
		nvps.add(new BasicNameValuePair("wackyAnswer", "sdfsdf"));
		//
		nvps.add(new BasicNameValuePair("submitButton", "Save"));

		try {
			httpostProfile.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			response = httpclient.execute(httpostProfile);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		entity = response.getEntity();
		System.out.println("Profile update: " + response.getStatusLine());
		if (entity != null) {
			try {
				System.out.println("Profile edit page: "
						+ EntityUtils.toString(entity));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				entity.consumeContent();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		httpclient.getConnectionManager().shutdown();


		
	}

	public void setCpo(CommonDataObject cpo) {
		this.cpo = cpo;
	}

	public CommonDataObject getCpo() {
		return cpo;
	}

}
